<?php
class PWP_Mdt {

	private static $keyTableName = "platformawp_key";
	
	public static function createTable() {
		global $wpdb;
		$sql = "CREATE TABLE IF NOT EXISTS ".self::getKeyTableName()."(site_key VARCHAR(41), PRIMARY KEY (site_key));";
		$wpdb->query($sql);
	}
	
	public static function dropTable() {
		global $wpdb;
		$sql = "DROP TABLE ".self::getKeyTableName().";";
		$wpdb->query($sql);
	}
	
	public function saveSiteKey($key) {
		global $wpdb;
		$sql = $wpdb->prepare("INSERT INTO ".self::getKeyTableName()." VALUES(%s);", $key);
		$wpdb->query($sql);
	}
	
	public function updateSiteKey($key) {
		global $wpdb;
		$sql = $wpdb->prepare("UPDATE ".self::getKeyTableName()." SET site_key(%s);", $key);
		$wpdb->query($sql);
	}
	
	public function siteKeyExist() {
		global $wpdb;
		$sql = "SELECT COUNT(*) FROM ".self::getKeyTableName();
		return $wpdb->get_var($sql) == 1;
	}
	
	public function getSiteKey() {
		global $wpdb;
		$sql = "SELECT site_key FROM ".self::getKeyTableName();
		return $wpdb->get_var($sql);
	}
	
	public function getComments() {
		global $wpdb;
		$commentsTable = $wpdb->prefix.'comments';
		$sql = "SELECT pst.post_title, cm.comment_author, cm.comment_date, cm.comment_content FROM $commentsTable".
			" as cm LEFT JOIN wp_posts pst ON cm.comment_post_ID=pst.ID";
		return $wpdb->get_results($sql);
	}
	
	private static function getKeyTableName() {
		global $wpdb;
		return $wpdb->prefix.self::$keyTableName;
	}
}
?>
